<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class PendataanSiswa extends MY_Controller {

	function __construct()
    {
		// Call the Model constructor
        parent::__construct();
		if($this->session->userdata('login') === TRUE && in_array(113,$this->access)){
			$log['activity']='VIEW';
			$log['information']='Masuk ke menu : '.$this->uri->uri_string();
			$this->model_log->add($log);
		}else{
			$log['activity']='VIEW';
			$log['information']='Tidak diijinkan : '.$this->uri->uri_string();
			$this->model_log->add($log);
			redirect('sessions/login');
		}
		
		$this->config->set_item("menu_active","data-siswa");
		$this->config->set_item("menu_root","kesiswaan");
    }
	
	public function index()
	{
		$mdl = $this->load->model('model_pendataan_siswa');
		$mdl2 = $this->load->model('departemen/model_departemen');
		$mdl3 = $this->load->model('tahunAjaran/model_tahun_ajaran');
		$mdl4 = $this->load->model('semester/model_semester');
		$mdl5 = $this->load->model('kelas/model_kelas');
	    $mdl6 = $this->load->model('pembagianKelas/model_pembagian_kelas');
		
		$departemen_id = 0;
		$tahun_ajaran_id = 0;
		$semester_id = 0;
		$kelas_id = 0;
		$pembagian_kelas_id = 0;
		
		$filter = false;
		$load = false;
		
		$data['departemen']= $mdl2->get_departemen(1);
		
		if(isset($_POST['departemen'])){
			$departemen_id = $_POST['departemen'];
			$tahun_ajaran_id = isset($_POST['tahun_ajaran'])?$_POST['tahun_ajaran']:0;
			$semester_id = isset($_POST['semester'])?$_POST['semester']:0;
			$kelas_id = isset($_POST['kelas'])?$_POST['kelas']:0;
			$pembagian_kelas_id = isset($_POST['pembagian_kelas'])?$_POST['pembagian_kelas']:0;
			
			if(isset($_POST['filter'])){
				$filter = true;
			}
		}else if($this->session->flashdata('departemen_id')){
			$departemen_id = $this->session->flashdata('departemen_id');
			$tahun_ajaran_id = $this->session->flashdata('tahun_ajaran_id');
			$semester_id = $this->session->flashdata('semester_id');
			$kelas_id = $this->session->flashdata('kelas_id');
			$pembagian_kelas_id = $this->session->flashdata('pembagian_kelas_id');
			
			$filter = true;
		}else{
			foreach($data['departemen']->result_array() as $row){
				$departemen_id = $row['departemen_id'];
				break;
			}
			$load = true;
		}
		
		$data['tahun_ajaran']= $mdl3->get_tahun_ajaran($departemen_id,1);
		$data['semester']= $mdl4->get_semester($departemen_id,1);
		$data['kelas']= $mdl5->get_kelas($departemen_id,1);
		
		if($load){
			foreach($data['kelas']->result_array() as $row){
				$kelas_id = $row['tingkat_id'];
				break;
			}
		}
		$data['pembagian_kelas']= $mdl6->get_all($kelas_id,1);
		
		if($filter){
			$data['siswa']= $mdl->get_siswa($departemen_id,$semester_id,$tahun_ajaran_id,$kelas_id,$pembagian_kelas_id);
		}
		
		$data['departemen_id'] = $departemen_id;
		$data['tahun_ajaran_id'] = $tahun_ajaran_id;
		$data['semester_id'] = $semester_id;
		$data['kelas_id'] = $kelas_id;
		$data['pembagian_kelas_id'] = $pembagian_kelas_id;
		
		$params['content'] = $this->load->view('list',$data,TRUE);
		$params['footer']['js_plugin'] = $this->load->view('js_plugin','',TRUE);
		$params['header']['css_plugin'] = $this->load->view('css_plugin','',TRUE);
		
		echo modules::run('template', $params);	
	}
	
	public function form($departemen_id,$semester_id,$tahun_ajaran_id,$kelas_id,$pembagian_kelas_id,$id=0)
	{
		$mdl_siswa = $this->load->model('model_pendataan_siswa');
		$mdl_angkatan = $this->load->model('angkatan/model_angkatan');
		$mdl_pembagian_kelas = $this->load->model('pembagianKelas/model_pembagian_kelas');
		$mdl_suku = $this->load->model('suku/model_suku');
		$mdl_bagian_pegawai = $this->load->model('bagianPegawai/model_bagian_pegawai');
		$mdl_agama = $this->load->model('agama/model_agama');
		$mdl_status_siswa = $this->load->model('statusSiswa/model_status_siswa');
		$mdl_kondisi_siswa = $this->load->model('kondisiSiswa/model_kondisi_siswa');
		$mdl_asal_sekolah = $this->load->model('asalSekolah/model_asal_sekolah');
		$mdl_tingkat_pendidikan = $this->load->model('tingkatPendidikan/model_tingkat_pendidikan');
		$mdl_jenis_pekerjaan = $this->load->model('jenisPekerjaan/model_jenis_pekerjaan');
		$mdl_departemen = $this->load->model('departemen/model_departemen');
		$mdl_semester = $this->load->model('semester/model_semester');
		$mdl_tahun_ajaran = $this->load->model('tahunAjaran/model_tahun_ajaran');
		$mdl_kelas = $this->load->model('kelas/model_kelas');
		
		if($id!=0){
			$id = decode($id);
		}
		$departemen_id = decode($departemen_id);
		$semester_id = decode($semester_id);
		$tahun_ajaran_id = decode($tahun_ajaran_id);
		$kelas_id = decode($kelas_id);		
		$pembagian_kelas_id = decode($pembagian_kelas_id);
		$jurusan_id = 0;
		$angkatan_id = 0;
		$suku_id = 0;
		$agama_id = 0;
		$status_siswa_id = 0;
		$kondisi_siswa_id = 0;
		$asal_sekolah_id = 0;
		$tingkat_pendidikan_id = 0;
		$jenis_pekerjaan_id = 0;
		
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			
			$d = $_POST['day'];
			$m = $_POST['month'];
			$y = $_POST['year'];
			$data['tgl_lahir'] = $y."-".$m.'-'.$d;
			
			if(!empty($_FILES) && $_FILES['userfile']['size'] != 0){
				$config['upload_path'] = './temp/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '100';
				$config['max_width']  = '1024';
				$config['max_height']  = '768';

				$this->load->library('upload', $config);

				if ( ! $this->upload->do_upload())
				{
					$this->session->set_flashdata('error',$this->upload->display_errors());
					redirect('pendataanSiswa/form/'.encode($id));
				}
				else
				{
					$upload = $this->upload->data();
					$data['foto'] = file_get_contents($upload['full_path']);
					
					if(is_file($upload['full_path']))
						unlink($upload['full_path']);
				}
			}
		
			if($id == 0){	
				$this->model_pendataan_siswa->add($data);
				
				$log['activity']='INSERT';
				$log['information']='Insert Data Siswa dengan data :'.json_encode($_POST['data']);
			}else{
				$this->model_pendataan_siswa->edit($id,$data);			
				$log['activity']='UPDATE';
				$log['information']="UPDATE Data Siswa id $id dengan data :".json_encode($_POST['data']);
			}
			
			$this->model_log->add($log);
			
			$this->session->set_flashdata('departemen_id', $departemen_id);
			$this->session->set_flashdata('semester_id', $semester_id);
			$this->session->set_flashdata('tahun_ajaran_id', $tahun_ajaran_id);
			$this->session->set_flashdata('kelas_id', $kelas_id);
			$this->session->set_flashdata('pembagian_kelas_id', $pembagian_kelas_id);
			
			redirect('pendataanSiswa');
			
			return;
		}
		
		$data['data']= $this->model_pendataan_siswa->get_pendataan_siswa_by($id);
		$data['departemen'] = $mdl_departemen->get_departemen();
		$data['semester'] = $mdl_semester->get_semester($departemen_id);
		$data['tahun_ajaran'] = $mdl_tahun_ajaran->get_tahun_ajaran($departemen_id);
		$data['kelas'] = $mdl_kelas->get_kelas($departemen_id);
		$data['angkatan']= $mdl_angkatan->get_angkatan($departemen_id);
		$data['pembagian_kelas']= $mdl_pembagian_kelas->get_all($kelas_id);
		$data['suku']= $mdl_suku->get_suku();
		$data['agama']= $mdl_agama->get_agama();
		$data['status_siswa']= $mdl_status_siswa->get_all();
		$data['kondisi_siswa']= $mdl_kondisi_siswa->get_all();
		$data['asal_sekolah'] = $mdl_asal_sekolah->get_all();
		$data['tingkat_pendidikan'] = $mdl_tingkat_pendidikan->get_all();
		$data['jenis_pekerjaan'] = $mdl_jenis_pekerjaan->get_all();	

		$data['departemen_id'] = $departemen_id;
		$data['semester_id'] = $semester_id;
		$data['tahun_ajaran_id'] = $tahun_ajaran_id;
		$data['pembagian_kelas_id'] = $pembagian_kelas_id;
		$data['angkatan_id'] = $angkatan_id;
		$data['kelas_id'] = $kelas_id;
		$data['suku_id'] = $suku_id;
		$data['agama_id'] = $agama_id;
		$data['status_siswa_id'] = $status_siswa_id;
		$data['kondisi_siswa_id'] = $kondisi_siswa_id;
		$data['asal_sekolah_id'] = $asal_sekolah_id;
		$data['tingkat_pendidikan_id'] = $tingkat_pendidikan_id;
		$data['jenis_pekerjaan_id'] = $jenis_pekerjaan_id;
		
		
		$params['content'] = $this->load->view('form',$data,TRUE);
		$params['footer']['js_plugin'] = $this->load->view('js_plugin','',TRUE);
		$params['header']['css_plugin'] = $this->load->view('css_plugin','',TRUE);
		
		echo modules::run('template', $params);	
	}
	
	public function delete($departemen_id,$semester_id,$tahun_ajaran_id,$kelas_id,$pembagian_kelas_id,$id=0){
		$this->load->model('model_pendataan_siswa');
		$mdl2 = $this->load->model('departemen/model_departemen');
		$mdl3 = $this->load->model('tahunAjaran/model_tahun_ajaran');
		$mdl4 = $this->load->model('kelas/model_kelas');
		$mdl5 = $this->load->model('pembagianKelas/model_pembagian_kelas');
		
		$id = decode($id);
		$departemen_id = decode($departemen_id);		
		$semester_id = decode($semester_id);
		$tahun_ajaran_id = decode($tahun_ajaran_id);
		$kelas_id = decode($kelas_id);
		$pembagian_kelas_id = decode($pembagian_kelas_id);
				
					
		$this->model_pendataan_siswa->delete($id);
		$this->session->set_flashdata('departemen_id', $departemen_id);
		$this->session->set_flashdata('semester_id', $semester_id);
		$this->session->set_flashdata('tahun_ajaran_id', $tahun_ajaran_id);
		$this->session->set_flashdata('kelas_id', $kelas_id);
		$this->session->set_flashdata('pembagian_kelas_id', $pembagian_kelas_id);
		
		redirect('pendataanSiswa');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */